//index.js
//获取应用实例
const app = getApp()

import { Base64 } from '../../miniprogram_npm/js-base64/index.js'

Page({
  onGetToken(){
    wx.login({
      success: (res)=> {
        console.log('------code--')
        console.log(res)
        if(res.code) {
          wx.request({
            url: 'http://localhost:4000/v1/token',
            method: 'POST',
            data: {
              account: res.code,
              type: 100
            },
            success: (res)=>{
              console.log('----获取token---')
              console.log(res)
              // token验证
              const code = res.statusCode.toString()
              if(code.startsWith('2')) {
                wx.setStorageSync('token', res.data.token)
              }
            }
          })
        }
      }
    })
  },

  onVerifyToken(){
    let token = wx.getStorageSync('token')
    wx.request({
      url: 'http://localhost:4000/v1/token/verify',
      method: 'POST',
      data: {
        token
      },
      success: (res) => {
        console.log('verify-----token')
        console.log(res)
      }
    })
  },
  onNewList() {
    wx.request({
      url: 'http://localhost:4000/v1/classic/latest',
      method: 'GET',
      success:(res) => {
        console.log('-----最新一期列表数据----')
        console.log(res)
      },
      header:{
        Authorization: this._encode()
      }
    })
  },
  onNextList() {
    wx.request({
      url: 'http://localhost:4000/v1/classic/1/next',
      method: 'GET',
      success: (res) => {
        console.log('-----最next一期列表数据----')
        console.log(res)
      },
      header: {
        Authorization: this._encode()
      }
    })
  },

  onPreList() {
    wx.request({
      url: 'http://localhost:4000/v1/classic/3/previous',
      method: 'GET',
      success: (res) => {
        console.log('-----最previous一期列表数据----')
        console.log(res)
      },
      header: {
        Authorization: this._encode()
      }
    })
  },

  onSomeOneList() {
    wx.request({
      url: 'http://localhost:4000/v1/classic/2/detail',
      method: 'GET',
      success: (res) => {
        console.log('-----最某一期列表数据----')
        console.log(res)
      },
      header: {
        Authorization: this._encode()
      }
    })
  },

  onSomeOneList2() {
    wx.request({
      url: 'http://localhost:4000/v1/classic/300/2',
      method: 'GET',
      success: (res) => {
        console.log('-----最某一期列表数据2----')
        console.log(res)
      },
      header: {
        Authorization: this._encode()
      }
    })
  },
  

  onLike() {
    wx.request({
      url: 'http://localhost:4000/v1/like',
      method: 'POST',
      data: {
        art_id: 1,
        type: 100
      },
      success: (res) => {
        console.log('-----点赞----')
        console.log(res)
      },
      header: {
        Authorization: this._encode()
      }
    })
  },
  onDisLike() {
    wx.request({
      url: 'http://localhost:4000/v1/like/cancel',
      method: 'POST',
      data: {
        art_id: 1,
        type: 100
      },
      success: (res) => {
        console.log('-----取消点赞----')
        console.log(res)
      },
      header: {
        Authorization: this._encode()
      }
    })
  },
  _encode(){
    // account: passwprd
    const token = wx.getStorageSync('token')
    const base64 = Base64.encode(token+ ':')

    return 'Basic ' +base64
  }

})
